package com.mandala.portlet;

import com.mandala.constants.SsoSystemManagePortletKeys;
import com.mandala.util.DateUtils;
import com.mandala.util.MD5;
import com.mandala.util.PropertiesUtil;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.Validator;

import java.io.IOException;

import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Component;

/**
 * @author michael
 */
@Component(
	immediate = true,
	property = {
		"com.liferay.portlet.display-category=category.collaboration",
		"com.liferay.portlet.instanceable=true",
		"javax.portlet.title=SsoSystemManage-portlet",
		"javax.portlet.display-name=SsoSystemManage-portlet",
		"javax.portlet.portlet-info.keywords=SsoSystemManage-portlet",
		"javax.portlet.portlet-info.short-title=SsoSystemManage-portlet",
		"javax.portlet.portlet-info.title=SsoSystemManage-portlet",
		"javax.portlet.init-param.template-path=/",
		"javax.portlet.init-param.view-template=/view.jsp",
		"javax.portlet.name=" + SsoSystemManagePortletKeys.SsoSystemManage,
		"javax.portlet.resource-bundle=content.Language",
		"javax.portlet.security-role-ref=power-user,user"
	},
	service = Portlet.class
)
public class SsoSystemManagePortlet extends MVCPortlet {
	
	 public static final String URL = "url";
	  public static final String USERID = "email";
	
	@Override
	public void doView(
			RenderRequest renderRequest, RenderResponse renderResponse)
		throws IOException, PortletException {

		//include(viewTemplate, renderRequest, renderResponse);
	  	putRequest(renderRequest, renderResponse);
		super.doView(renderRequest, renderResponse);
	}
	
	  /**
		 * 设置preferece
		 */
		public void putRequest(RenderRequest renderRequest,
				RenderResponse renderResponse){
			try {
				User user = PortalUtil.getUser(renderRequest);
				if(Validator.isNotNull(user)){
					String email = user.getEmailAddress();
					String key = DateUtils.getCurrDateStr();
					String token = MD5.getDigestedString(email + key);
					renderRequest.setAttribute("userToken", token);
					renderRequest.setAttribute(USERID, email);
				}
				String url = PropertiesUtil.getText("SsoSystemConfig.iframe.url");
				renderRequest.setAttribute(URL, url);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}	
}